package com.cheyun.netsalev3.view.mars.choosecar

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.databinding.MarsChooseCarActivityBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.MarsChooseCarViewModel
import java.io.Serializable

class MarsChooseCarActivity : BaseActivity<MarsChooseCarActivityBinding>() {
    override fun initContentView(): Int {
        return R.layout.mars_choose_car_activity
    }

    var pos = 0
    var carBrandParam: CarBrandParam? = null
    var level = 0
    var drive = false
    var stime = ""
    var enableClickAll = false
    var frozenSeries = false

    override fun initViewModel(): ViewModel? {
        pos = intent.getIntExtra("pos", 0)
        level = intent.getIntExtra("level", 0)
        if(intent.hasExtra("drive")) {
            drive = intent.getBooleanExtra("drive",false)
        }
        if(intent.hasExtra("stime")) {
            stime = intent.getStringExtra("stime")
        }
        if(intent.hasExtra("enableClickAll")) {
            enableClickAll = intent.getBooleanExtra("enableClickAll",false)
        }
        if(intent.hasExtra("frozenSeries")){
            frozenSeries = intent.getBooleanExtra("frozenSeries",false)
        }
        intent.getSerializableExtra("car")?.apply {
            carBrandParam = this as CarBrandParam
            var factory = InjectorUtils.getMarsChooseCarViewModelFactory(carBrandParam!!,drive,stime,enableClickAll)
            return ViewModelProviders.of(this@MarsChooseCarActivity, factory).get(MarsChooseCarViewModel::class.java)
        }
        return null
    }

    private var listFragmet = ArrayList<Fragment>()
    override fun initView() {
        listFragmet.add(MarsChooseCarFragment2.newInstance("1"))
        listFragmet.add(MarsChooseCarFragment2.newInstance("2"))
        listFragmet.add(MarsChooseCarFragment2.newInstance("3"))
        binding.viewPager.offscreenPageLimit = 3

        binding.viewPager.adapter = HomePageAdapter(supportFragmentManager, listFragmet)
        binding.headView.rightBut?.setOnClickListener {
            var intent = Intent()
            intent.putExtra("car", binding.viewModel?.carBrandParam as Serializable)
            if(level == 0)
                setResult(102, intent)
            else if(level == 1)
                setResult(104, intent)
            else if(level == 2){
                setResult(105, intent)
            }
            finish()
        }

        var notop = intent.getBooleanExtra("notop", false)
        if (notop) {
            binding.topView.visibility = View.GONE
        }
        binding.viewModel?.setIsbrand(intent.getBooleanExtra("Isbrand", false))
        binding.viewModel?.setIsseries(intent.getBooleanExtra("Isseries", false))
        binding.viewModel?.setIsspec(intent.getBooleanExtra("Isspec", false))
        binding.viewModel?.setLevel(level)
    }

    override fun initData() {
        carBrandParam?.apply {
            when (pos) {
                1 -> {
                    if (!this.brand.isEmpty()) {
                        binding.viewModel?.initData(1)
                        binding.viewPager.currentItem = 0
                    }
                }
                2 -> {
                    if (!this.series.isEmpty()) {
                        binding.viewModel?.initData(2)
                        binding.viewPager.currentItem = 1
                    }
                }
                3 -> {
                    if (!this.spec.isEmpty()) {
                        binding.viewModel?.initData(3)
                        binding.viewPager.currentItem = 2
                    }
                }
            }

        }
        //车系
        binding.brandBut.setOnClickListener {
            if(!frozenSeries) {
                binding.viewPager.currentItem = 0
            }
        }
        //车型
        binding.seriesBut.setOnClickListener {
            binding.viewModel?.carBrandParam?.apply {
                if (this.series == "") {
                    Toast.makeText(this@MarsChooseCarActivity, "请选择车系", Toast.LENGTH_LONG).show()
                } else {
                    binding.viewModel?.initData(2)
                    binding.viewPager.currentItem = 1
                }
            }

        }
        //产品
        binding.specBut.setOnClickListener {
            binding.viewModel?.carBrandParam?.apply {
                if (this.spec == "") {
                    Toast.makeText(this@MarsChooseCarActivity, "请选择车型", Toast.LENGTH_LONG).show()
                } else {
                    binding.viewModel?.initData(3)
                    binding.viewPager.currentItem = 2
                }
            }
        }

        if(frozenSeries){
            binding.viewModel?.carBrandParam?.apply {
                if (this.series == "") {
                    Toast.makeText(this@MarsChooseCarActivity, "请选择车系", Toast.LENGTH_LONG).show()
                } else {
                    binding.viewModel?.initData(2)
                    binding.viewPager.currentItem = 1
                }
            }
        }
    }


}
